package com.liu.ssm.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.liu.ssm.controller.service.ClazzService;
import com.liu.ssm.controller.service.GradeService;
import com.liu.ssm.entity.Clazz;
import com.liu.ssm.entity.Grade;
import com.liu.ssm.page.Page;
import com.liu.ssm.util.StringUtil;

import net.sf.json.JSONArray;

/**
 * 班级信息管理
 * 
 * @author SHIYI
 *
 */
@RequestMapping("/clazz")
@Controller
public class ClazzController {

	@Autowired
	private GradeService gradeservice;
	@Autowired
    private ClazzService clazzservice;
	/**
	 * 班级管理列表页面
	 * 
	 * @param modviw
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView modviw) {
		modviw.setViewName("clazz/clazz_list");
		List<Grade> findAll = gradeservice.findAll();
		modviw.addObject("gradeList",findAll);
		modviw.addObject("gradeListJson",JSONArray.fromObject(findAll));
		return modviw;
	}

	/**
	 * 获取班级列表
	 * 
	 * @param name
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/get_list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(
			@RequestParam(value="name",required=false,defaultValue="") String name,
			@RequestParam(value="gradeId",required=false) Long gradeId,
			Page page
			){
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("name", "%"+name+"%");
		if(gradeId != null){
			queryMap.put("gradeId", gradeId);
		}
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());
		ret.put("rows", clazzservice.findList(queryMap));
		ret.put("total", clazzservice.getTotal(queryMap));
		return ret;
	}

	/**
	 * 添加班级信息
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> add(Clazz clazz) {
		Map<String, String> ret = new HashMap<String, String>();
		if (StringUtils.isEmpty(clazz.getName())) {
			ret.put("type", "error");
			ret.put("msg", "班级名称不能为空！");
			return ret;
		}
		if (clazz.getGradeId() == null) {
			ret.put("type", "error");
			ret.put("msg", "请选择所属年级！");
			return ret;
		}
		if (clazzservice.add(clazz) <= 0) {
			ret.put("type", "error");
			ret.put("msg", "班级添加失败！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "班级添加成功！");
		return ret;
	}

	/**
	 * 修改班级信息
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> edit(Clazz clazz) {
		Map<String, String> ret = new HashMap<String, String>();
		if (StringUtils.isEmpty(clazz.getName())) {
			ret.put("type", "error");
			ret.put("msg", "班级名称不能为空！");
			return ret;
		}
		if (clazz.getGradeId() == null) {
			ret.put("type", "error");
			ret.put("msg", "所属年级不能为空！");
			return ret;
		}
		if (clazzservice.edit(clazz) <= 0) {
			ret.put("type", "error");
			ret.put("msg", "班级修改失败！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "班级修改成功！");
		return ret;
	}

	/**
	 * 删除班级信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(
			@RequestParam(value="ids[]",required=true) Long[] ids
			){
		Map<String, String> ret = new HashMap<String, String>();
		if(ids == null || ids.length == 0){
			ret.put("type", "error");
			ret.put("msg", "请选择要删除的数据！");
			return ret;
		}
		try {
			if(clazzservice.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
				ret.put("type", "error");
				ret.put("msg", "删除失败！");
				return ret;
			}
		} catch (Exception e) {
			// TODO: handle exception
			ret.put("type", "error");
			ret.put("msg", "该班级下存在学生信息，请勿冲动！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "班级删除成功！");
		return ret;
	}

}
